package com.smartwebx.ha.web.gis.module.screen;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.biz.rainwater.real.GrwManager;
import com.smartwebx.ha.dal.annot.JsonModule;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.GisWater;
import com.smartwebx.ha.dal.usebean.view.ViewProcessBean;
import com.smartwebx.ha.web.admin.common.HaUserSys;
import com.smartwebx.ha.web.util.json.module.DateModule;
import com.smartwebx.ha.web.util.valve.MositrueAndGroundWaterChart;

public class GroundWaterScreen extends BasicScreen {
	@Autowired
	private GrwManager grwManager;
	@Autowired
	private HaAreaManager haAreaManager;
	@Autowired
	private HttpSession session;

	/**
	 * 最新地下水GIS信息
	 * 
	 * @param tm
	 *            (2014-02-25)
	 * @return
	 * @throws ParseException
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd")
	public List<GisWater> getLatestRiverGisInfo(Date tm) throws ParseException {
		HaUserSys loginUser = (HaUserSys) session.getAttribute("haUser");
		HaArea area=haAreaManager.findOne(loginUser.getAreaId());
		List<GisWater> dateValues = grwManager.getAllStatGroundWater(tm,area);
		return dateValues;
	}

	/**
	 * 地下水日过程(zx)
	 * 
	 * @param stcd
	 *            (50939900)
	 * @param bTm
	 *            (2014-09-11)
	 * @param eTm
	 *            (2014-09-26)
	 * @param dxn
	 *            (2010)
	 * @return
	 * @throws ParseException
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm")
	public List<ViewProcessBean> getSingleStatDayProcess(String stcd, Date bTm,
			Date eTm, Integer dxn) throws Exception {
		List<DateValue> dateValueWaters = grwManager.getPurenessWaterProcesss(
				stcd, bTm, eTm);// 地下水日水位过程
		List<ViewProcessBean> viewProcessBeans = new ArrayList<ViewProcessBean>();
		for (DateValue dateValueWater : dateValueWaters) {
			ViewProcessBean viewProcessBean = new ViewProcessBean();
			BeanUtils.copyProperties(viewProcessBean, dateValueWater);
			viewProcessBeans.add(viewProcessBean);
		}
		return viewProcessBeans;
	}
	
	public Object getSingleStatDayProcessByGisJs(String stcd, Date bTm,
			Date eTm, Integer dxn) throws Exception {
		List<DateValue> dateValueWaters = grwManager.getPurenessWaterProcesss(
				stcd, bTm, eTm);// 地下水日水位过程
		List<Double> vals=Lists.newArrayList();//水位
		List<Date> valDates=Lists.newArrayList();//时间
		for (DateValue dateValueWater : dateValueWaters) {
			vals.add(dateValueWater.getVal());
			valDates.add(dateValueWater.getTm());
		}
		MositrueAndGroundWaterChart mositrueAndGroundWaterChart = new MositrueAndGroundWaterChart();
		Map<String,List<Double>> result = ImmutableMap.of("vals",vals);
		mositrueAndGroundWaterChart.setResult(result);
		if(valDates.size()>0){
			mositrueAndGroundWaterChart.setStartPoint(valDates.get(0));
		}
		return mositrueAndGroundWaterChart;
	}
}
